package com.gzhryc.jdbc;

import com.gzhryc.jdbc.annotaion.Column;
import com.gzhryc.jdbc.annotaion.Id;
import com.gzhryc.jdbc.log.Logger;
import com.gzhryc.jdbc.models.LRTree;
import com.gzhryc.jdbc.models.Parameters;
import com.gzhryc.jdbc.utils.EntityUtils;

import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class LRTreeBaseDao<T extends LRTree<R>, R> extends JoinSelectDao<T> {

	private static Logger log = Logger.getLogger(LRTreeBaseDao.class);

	public LRTreeBaseDao() {
		super(null);
	}

	public LRTreeBaseDao(String jdbcKey) {
		super(jdbcKey);
	}

	public String getJdbcKey() {
		return this.jdbcHelper.getJdbcKey();
	}

	public T getById(R id) throws SQLException {
		if (id != null && idField != null) {
			String idName = EntityUtils.getName(idField);
			Query query = new Query();
			query.setSql("select * from " + tableName + " where " + idName + "=?");
			query.getParams().setObject(id);
			return this.jdbcHelper.get(query, clazz);
		} else {
			throw new SQLException("id is Null");
		}
	}

	public T get(Conditions condition) throws SQLException {
		Query query = new Query();
		query.setSql("select * from " + tableName);
		query.appendSql(condition.toSql(query.getParams(), true));
		log.debug("sql:" + query.getSql());
		return this.jdbcHelper.get(query, clazz);
	}

	public List<T> findByCondition(Conditions condition) throws SQLException {
		Query query = new Query();
		query.setSql("select * from " + tableName);
		query.appendSql(condition.toSql(query.getParams(), true));
		return this.jdbcHelper.find(query, clazz);
	}

	/**
	 * 分页查询数据
	 * 
	 * @param condition
	 * @param pageNo
	 * @param pageLimit
	 * @return
	 * @throws SQLException
	 */
	public List<T> findByCondition(Conditions condition, Integer pageNo, Integer pageLimit) throws SQLException {
		Query query = new Query();
		query.setSql("SELECT * FROM " + tableName);
		query.appendSql(condition.toSql(query.getParams(), true));
		query.setPageNo(pageNo);
		query.setPageLimit(pageLimit);
		return this.jdbcHelper.find(query, clazz);
	}

	public Long count(Conditions condition) throws SQLException {
		Query query = new Query("SELECT COUNT(1) AS total FROM " + tableName);
		query.appendSql(condition.toSql(query.getParams(), true));
		Map<String, Object> result = this.jdbcHelper.get(query);
		return Long.valueOf(result.get("total").toString());
	}

	public int insert(T obj) throws SQLException {
		JdbcSession.begin();
		try {
			R parentId = obj.getParentId();
			if (parentId != null) {
				LRTree<R> parent = this.getById(parentId);
				if (parent != null) {
					obj.setRootId(parent.getRootId());
					obj.setLeftNum(parent.getLeftNum() + 1);
					obj.setRightNum(obj.getLeftNum() + 1);

					addLeftAndRight(parent.getRootId(), parent.getLeftNum(), parent.getRightNum(), 2L);
					int count = jdbcHelper.insert(tableName,obj);
					if (count > 0) {
						JdbcSession.commit();
						return count;
					}
				}
			} else {
				obj.setLeftNum(1L);
				obj.setRightNum(2L);
				int result = jdbcHelper.insert(tableName,obj);
				if (result > 0) {
					updateRootId(obj.getId(), obj.getId());
					JdbcSession.commit();
					return result;
				}
			}
		} finally {
			JdbcSession.end();
		}
		return 0;
	}

	public int update(T obj) throws SQLException {
		JdbcSession.begin();
		try {
			LRTree<R> temp = this.getById(obj.getId());
			if (temp != null) {
				obj.setRootId(temp.getRootId());
				obj.setLeftNum(temp.getLeftNum());
				obj.setRightNum(temp.getRightNum());

				boolean flag = false;		//是否修改父节点
				if (obj.getParentId() != null) {
					if (temp.getParentId() == null || !obj.getParentId().equals(temp.getParentId())) {
						flag = true;
					}
				} else if (temp.getParentId() != null) {
					flag = true;
				}

				if (flag) {
					T parent1 = null;
					T parent2 = null;

					if (obj.getParentId() != null) {
						parent1 = this.getById(obj.getParentId());
					}

					if (temp.getParentId() != null) {
						parent2 = this.getById(temp.getParentId());
					}

					if (parent1 == null) {
						updateRootId(temp.getRootId(), temp.getLeftNum(), temp.getRightNum(), obj.getId(), 0L);
						if (parent2 != null) {
							Long num = temp.getRightNum() - temp.getLeftNum() + 1;
							minusLeftAndRight(parent2.getRootId(), temp.getLeftNum(), temp.getRightNum(), num);
						}
					} else if (parent2 != null) {
						if (parent1.getRootId().equals(parent2.getRootId())) {
							// 同一个树
							updateRootId(temp.getRootId(), temp.getLeftNum(), temp.getRightNum(), obj.getId(), 0L);
							Long num = temp.getRightNum() - temp.getLeftNum() + 1;
							minusLeftAndRight(parent2.getRootId(), temp.getLeftNum(), temp.getRightNum(), num);

							parent1 = this.getById(obj.getParentId());
							addLeftAndRight(parent1.getRootId(), parent1.getLeftNum(), parent1.getRightNum(), num);
							temp = getById(temp.getId());
							updateRootId(temp.getRootId(), temp.getLeftNum(), temp.getRightNum(), parent1.getRootId(), parent1.getLeftNum());
						} else {
							// 不同个树
							Long num = temp.getRightNum() - temp.getLeftNum() + 1;
							addLeftAndRight(parent1.getRootId(), parent1.getLeftNum(), parent1.getRightNum(), num);
							updateRootId(temp.getRootId(), temp.getLeftNum(), temp.getRightNum(), parent1.getRootId(), parent1.getLeftNum());
							minusLeftAndRight(parent2.getRootId(), temp.getLeftNum(), temp.getRightNum(), num);
						}
					} else {
						Long num = temp.getRightNum() - temp.getLeftNum() + 1;
						addLeftAndRight(parent1.getRootId(), parent1.getLeftNum(), parent1.getRightNum(), num);
						updateRootId(temp.getRootId(), temp.getLeftNum(), temp.getRightNum(), parent1.getRootId(), parent1.getLeftNum());
					}

					if (obj.getParentId() != null) {
						//获取新的参数
						temp = getById(obj.getId());
						obj.setRootId(temp.getRootId());
						obj.setLeftNum(temp.getLeftNum());
						obj.setRightNum(temp.getRightNum());
					}
				}

				if (obj.getParentId() == null) {
					obj.setRootId(temp.getId());
					//检查是否有下级，如果没有则初始化左右差值
					Conditions conditions = new Conditions();
					conditions.and().is("parent_id",obj.getId());
					Long total = this.count(conditions);
					if(total == null && total <= 0) {
						obj.setLeftNum(1L);
						obj.setRightNum(2L);
					}
				}
			} else {
				throw new SQLException("未找到实体对象");
			}

			Parameters params = new Parameters();
			List<Field> fields = EntityUtils.findTableColumn(obj.getClass());
			String sql = "UPDATE " + tableName + " SET ";
			StringBuilder fieldStr = new StringBuilder();
			for (int i = 0; i < fields.size(); i++) {
				Field field = fields.get(i);
				if (idField != null && field.getName().equals(idField.getName())) {
					continue;
				}
				Object value = field.get(obj);
				String name = EntityUtils.getName(field);
				if (value != null) {
					fieldStr.append(",").append(name).append("=?");
					params.setField(field, value);
				} else {
					fieldStr.append(",").append(name).append("=null");
				}
			}

			if (fieldStr.length() > 1) {
				String name = EntityUtils.getName(idField);
				sql = sql + fieldStr.substring(1) + " WHERE " + name + "=?";
				params.setField(idField, idField.get(obj));
				int count = jdbcHelper.update(sql, params);
				if (count > 0) {
					JdbcSession.commit();
					return count;
				}
			}
		} catch (IllegalArgumentException | IllegalAccessException e) {
			throw new SQLException(e.fillInStackTrace());
		} finally {
			JdbcSession.end();
		}
		return 0;
	}

	public int update(Map<String, Object> data) throws SQLException {
		try {
			data.remove("rootId");
			data.remove("leftNum");
			data.remove("rightNum");
			data.remove("parentId");

			Object idValue = data.get(idField.getName());
			if (idValue == null) {
				return 0;
			}

			Parameters params = new Parameters();
			List<Field> fields = EntityUtils.findTableColumn(clazz);
			String sql = "UPDATE " + tableName + " SET ";
			StringBuilder fieldStr = new StringBuilder();
			for (int i = 0; i < fields.size(); i++) {
				Field field = fields.get(i);
				// 只支持Column注释的属性
				if (field.isAnnotationPresent(Column.class)) {
					if (idField != null && field.getName().equals(idField.getName())) {
						continue;
					}
					// 检查是否存在
					if (data.containsKey(field.getName())) {
						Object value = data.get(field.getName());
						String name = EntityUtils.getName(field);
						if (value != null) {
							fieldStr.append(",").append(name).append("= ?");
							params.setField(field, value);
						} else {
							fieldStr.append(",").append(name).append("= null");
						}
					}
				}
			}

			assert idField != null;
			String name = EntityUtils.getName(idField);
			sql = sql + fieldStr.substring(1) + " WHERE " + name + "= ?";
			params.setField(idField, idValue);
			return jdbcHelper.update(sql, params);
		} catch (IllegalArgumentException e) {
			throw new SQLException(e.fillInStackTrace());
		}
	}

	public int updateNotNull(T obj) throws SQLException {
		obj.setRootId(null);
		obj.setLeftNum(null);
		obj.setRightNum(null);
		obj.setParentId(null);
		try {
			Parameters params = new Parameters();
			List<Field> fields = EntityUtils.findTableColumn(obj.getClass());
			String sql = "UPDATE " + tableName + " SET ";
			StringBuilder fieldStr = new StringBuilder();
			for (int i = 0; i < fields.size(); i++) {
				Field field = fields.get(i);
				if (idField != null && field.getName().equals(idField.getName())) {
					continue;
				}
				Object value = field.get(obj);
				if (value != null) {
					String name = EntityUtils.getName(field);
					fieldStr.append(",").append(name).append("=?");
					params.setField(field, value);
				}
			}

			if (fieldStr.length() > 1) {
				String name = EntityUtils.getName(idField);
				sql = sql + fieldStr.substring(1) + " WHERE " + name + "=?";
				params.setField(idField, idField.get(obj));
				return jdbcHelper.update(sql, params);
			}
		} catch (IllegalArgumentException | IllegalAccessException e) {
			throw new SQLException(e.fillInStackTrace());
		}
		return 0;
	}

	public int updateNotNull(T obj, Conditions condition) throws SQLException {
		obj.setRootId(null);
		obj.setLeftNum(null);
		obj.setRightNum(null);
		obj.setParentId(null);
		try {
			Parameters params = new Parameters();
			List<Field> fields = EntityUtils.findTableColumn(obj.getClass());
			String sql = "UPDATE " + tableName + " SET ";
			StringBuilder fieldStr = new StringBuilder();
			for (int i = 0; i < fields.size(); i++) {
				Field field = fields.get(i);
				if (idField != null && field.getName().equals(idField.getName())) {
					continue;
				}
				Object value = field.get(obj);
				if (value != null) {
					String name = EntityUtils.getName(field);
					fieldStr.append(",").append(name).append("=?");
					params.setField(field, value);
				}
			}

			sql = sql + fieldStr.substring(1) + condition.toSql(params, true);
			return jdbcHelper.update(sql, params);
		} catch (IllegalArgumentException | IllegalAccessException e) {
			throw new SQLException(e.fillInStackTrace());
		}
	}

	public int updateParentAndNotNull(T obj) throws SQLException {
		JdbcSession.begin();
		try {
			obj.setRootId(null);
			obj.setLeftNum(null);
			obj.setRightNum(null);
			LRTree<R> temp = this.getById(obj.getId());
			if (temp != null) {
				boolean flag = false;
				if (obj.getParentId() != null) {
					if (temp.getParentId() == null || !obj.getParentId().equals(temp.getParentId())) {
						flag = true;
					}
				} else if (temp.getParentId() != null) {
					flag = true;
				}

				if (flag) {
					T parent1 = null;
					T parent2 = null;

					if (obj.getParentId() != null) {
						parent1 = this.getById(obj.getParentId());
					}

					if (temp.getParentId() != null) {
						parent2 = this.getById(temp.getParentId());
					}

					if (parent1 == null) {
						updateRootId(temp.getRootId(), temp.getLeftNum(), temp.getRightNum(), obj.getId(), 0L);
						if (parent2 != null) {
							Long num = temp.getRightNum() - temp.getLeftNum() + 1;
							minusLeftAndRight(parent2.getRootId(), temp.getLeftNum(), temp.getRightNum(), num);
						}
					} else if (parent2 != null) {
						if (parent1.getRootId().equals(parent2.getRootId())) {
							// 同一个树
							updateRootId(temp.getRootId(), temp.getLeftNum(), temp.getRightNum(), obj.getId(), 0L);
							Long num = temp.getRightNum() - temp.getLeftNum() + 1;
							minusLeftAndRight(parent2.getRootId(), temp.getLeftNum(), temp.getRightNum(), num);

							parent1 = this.getById(obj.getParentId());
							addLeftAndRight(parent1.getRootId(), parent1.getLeftNum(), parent1.getRightNum(), num);
							temp = getById(temp.getId());
							updateRootId(temp.getRootId(), temp.getLeftNum(), temp.getRightNum(), parent1.getRootId(), parent1.getLeftNum());
						} else {
							// 不同个树
							Long num = temp.getRightNum() - temp.getLeftNum() + 1;
							addLeftAndRight(parent1.getRootId(), parent1.getLeftNum(), parent1.getRightNum(), num);
							updateRootId(temp.getRootId(), temp.getLeftNum(), temp.getRightNum(), parent1.getRootId(), parent1.getLeftNum());
							minusLeftAndRight(parent2.getRootId(), temp.getLeftNum(), temp.getRightNum(), num);
						}
					} else {
						Long num = temp.getRightNum() - temp.getLeftNum() + 1;
						addLeftAndRight(parent1.getRootId(), parent1.getLeftNum(), parent1.getRightNum(), num);
						updateRootId(temp.getRootId(), temp.getLeftNum(), temp.getRightNum(), parent1.getRootId(), parent1.getLeftNum());
					}
				}
			} else {
				throw new SQLException("未找到实体对象");
			}

			Parameters params = new Parameters();
			List<Field> fields = EntityUtils.findTableColumn(obj.getClass());
			String sql = "UPDATE " + tableName + " SET ";
			StringBuilder fieldStr = new StringBuilder();
			for (int i = 0; i < fields.size(); i++) {
				Field field = fields.get(i);
				if (idField != null && field.getName().equals(idField.getName())) {
					continue;
				}
				Object value = field.get(obj);
				if (value != null) {
					String name = EntityUtils.getName(field);
					fieldStr.append(",").append(name).append("=?");
					params.setField(field, value);
				} else if (obj.getParentId() == null) {
					fieldStr.append(",parent_id = null");
				}
			}

			if (fieldStr.length() > 1) {
				String name = EntityUtils.getName(idField);
				sql = sql + fieldStr.substring(1) + " WHERE " + name + "=?";
				params.setField(idField, idField.get(obj));
				int count = jdbcHelper.update(sql, params);
				if (count > 0) {
					JdbcSession.commit();
					return count;
				}
			}
		} catch (IllegalArgumentException | IllegalAccessException e) {
			throw new SQLException(e.fillInStackTrace());
		} finally {
			JdbcSession.end();
		}
		return 0;
	}

	public int updateNotNullAndInc(T obj, Conditions condition,String... incFields) throws SQLException {
		try {
			obj.setRootId(null);
			obj.setLeftNum(null);
			obj.setRightNum(null);
			Parameters params = new Parameters();
			List<Field> fields = EntityUtils.findTableColumn(obj.getClass());
			StringBuilder sql = new StringBuilder("UPDATE " + tableName + " SET ");
			StringBuilder fieldStr = new StringBuilder();
			for (int i = 0; i < fields.size(); i++) {
				Field field = fields.get(i);
				if (idField != null && field.getName().equals(idField.getName())) {
					Id id = idField.getAnnotation(Id.class); // 如果自动生成则不进行修改
					if (id.auto()) {
						continue;
					}
				}
				Object value = field.get(obj);
				if (value != null) {
					String name = EntityUtils.getName(field);
					boolean flag = false;
					if(incFields != null && incFields.length > 0) {
						for (String temp : incFields) {
							if (temp.equals(field.getName())) {
								flag = true;
								break;
							}
						}
					}
					if (flag) {
						fieldStr.append(",").append(name).append("=").append(name).append("+ ?");
					}else{
						fieldStr.append(",").append(name).append("=?");
					}
					params.setField(field, value);
				}
			}

			if (fieldStr.length() > 0) {
				sql.append(fieldStr.substring(1));
			}
			sql.append(condition.toSql(params, true));
			return this.jdbcHelper.update(sql.toString(), params);
		} catch (IllegalArgumentException | IllegalAccessException e) {
			throw new SQLException(e.fillInStackTrace());
		}
	}

	public int updateNotNullAndInc(T obj, String... incFields) throws SQLException {
		try {
			if (idField == null) {
				return 0;
			}
			Object idVal = idField.get(obj);
			if (idVal == null) {
				return 0;
			}
			obj.setRootId(null);
			obj.setLeftNum(null);
			obj.setRightNum(null);
			Parameters params = new Parameters();
			List<Field> fields = EntityUtils.findTableColumn(obj.getClass());
			String sql = "update " + tableName + " set ";

			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < fields.size(); i++) {
				Field field = fields.get(i);
				if (field.getName().equals(idField.getName())) {
					continue;
				}
				Object value = field.get(obj);
				if (value != null) {
					String name = EntityUtils.getName(field);
					boolean flag = false;
					if(incFields != null && incFields.length > 0) {
						for (String temp : incFields) {
							if (temp.equals(field.getName())) {
								flag = true;
								break;
							}
						}
					}
					if (flag) {
						sb.append(",").append(name).append("=").append(name).append("+ ?");
					}else{
						sb.append(",").append(name).append("=?");
					}
					params.setField(field, value);
				}
			}

			if (sb.length() > 1) {
				String name = EntityUtils.getName(idField);
				sql = sql + sb.substring(1) + " where " + name + "=?";
				params.setField(idField, idVal);
				return jdbcHelper.update(sql, params);
			}
		} catch (IllegalArgumentException | IllegalAccessException e) {
			throw new SQLException(e.fillInStackTrace());
		}
		return 0;
	}

	public int updateNotNullAndReduce(T obj, String... incFields) throws SQLException {
		try {
			if (idField == null) {
				return 0;
			}
			Object idVal = idField.get(obj);
			if (idVal == null) {
				return 0;
			}
			obj.setRootId(null);
			obj.setLeftNum(null);
			obj.setRightNum(null);
			Parameters params = new Parameters();
			List<Field> fields = EntityUtils.findTableColumn(obj.getClass());
			String sql = "update " + tableName + " set ";

			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < fields.size(); i++) {
				Field field = fields.get(i);
				if (field.getName().equals(idField.getName())) {
					continue;
				}
				Object value = field.get(obj);
				if (value != null) {
					String name = EntityUtils.getName(field);
					boolean flag = false;
					if(incFields != null && incFields.length > 0) {
						for (String temp : incFields) {
							if (temp.equals(field.getName())) {
								flag = true;
								break;
							}
						}
					}
					if (flag) {
						sb.append(",").append(name).append("=").append(name).append("- ?");
					}else{
						sb.append(",").append(name).append("=?");
					}
					params.setField(field, value);
				}
			}

			if (sb.length() > 1) {
				String name = EntityUtils.getName(idField);
				sql = sql + sb.substring(1) + " where " + name + "=?";
				params.setField(idField, idVal);
				return jdbcHelper.update(sql, params);
			}
		} catch (IllegalArgumentException | IllegalAccessException e) {
			throw new SQLException(e.fillInStackTrace());
		}
		return 0;
	}

	public int updateNotNullAndReduce(T obj, Conditions condition,String... incFields) throws SQLException {
		try {
			obj.setRootId(null);
			obj.setLeftNum(null);
			obj.setRightNum(null);
			Parameters params = new Parameters();
			List<Field> fields = EntityUtils.findTableColumn(obj.getClass());
			StringBuilder sql = new StringBuilder("update " + tableName + " set ");
			StringBuilder fieldStr = new StringBuilder();
			for (int i = 0; i < fields.size(); i++) {
				Field field = fields.get(i);
				if (idField != null && field.getName().equals(idField.getName())) {
					Id id = idField.getAnnotation(Id.class); // 如果自动生成则不进行修改
					if (id.auto()) {
						continue;
					}
				}
				Object value = field.get(obj);
				if (value != null) {
					String name = EntityUtils.getName(field);
					boolean flag = false;
					if(incFields != null && incFields.length > 0) {
						for (String temp : incFields) {
							if (temp.equals(field.getName())) {
								flag = true;
								break;
							}
						}
					}
					if (flag) {
						fieldStr.append(",").append(name).append("=").append(name).append("- ?");
					}else{
						fieldStr.append(",").append(name).append("=?");
					}
					params.setField(field, value);
				}
			}

			if (fieldStr.length() > 0) {
				sql.append(fieldStr.substring(1));
			}
			sql.append(condition.toSql(params, true));
			return this.jdbcHelper.update(sql.toString(), params);
		} catch (IllegalArgumentException | IllegalAccessException e) {
			throw new SQLException(e.fillInStackTrace());
		}
	}

	/**
	 * 根据ID删除数据
	 * 
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public int deleteById(R id) throws SQLException {
		LRTree<R> tree = this.getById(id);
		if (tree != null && tree.getRootId() != null && tree.getLeftNum() != null && tree.getRightNum() != null) {
			JdbcSession.begin();
			try {
				Parameters params = new Parameters();
				params.setObject(tree.getRootId());
				params.setObject(tree.getLeftNum());
				params.setObject(tree.getRightNum());
				String sql = "DELETE FROM " + tableName + " WHERE root_id = ? AND left_num >= ? AND right_num <= ?";
				int result = jdbcHelper.update(sql, params);
				if (result > 0) {
					if (tree.getParentId() != null) {
						T parent = this.getById(tree.getParentId());
						if (parent != null) {
							Long num = tree.getRightNum() - tree.getLeftNum() + 1;
							minusLeftAndRight(parent.getRootId(), tree.getLeftNum(), tree.getRightNum(), num);
						}
					}
					JdbcSession.commit();
				}
				return result;
			} finally {
				JdbcSession.end();
			}
		}

		throw new SQLException("is not table entity");
	}

	/**
	 * 新增节点
	 * 
	 * @param rootId
	 * @param leftNum 父节点LeftNum
	 * @param addNum  新增值
	 * @throws SQLException
	 */
	public void addLeftAndRight(Object rootId, Long leftNum, Long rightNum, Long addNum) throws SQLException {
		String sql = "UPDATE " + tableName + " SET right_num = right_num + " + addNum + " WHERE root_id = ? AND (left_num >= ? OR right_num > ?)";

		Parameters paream = new Parameters();
		paream.setObject(rootId);
		paream.setObject(leftNum);
		paream.setObject(rightNum);
		this.jdbcHelper.update(sql, paream);

		sql = "UPDATE " + tableName + " SET left_num = left_num + " + addNum + " WHERE root_id = ? AND left_num > ?";

		paream = new Parameters();
		paream.setObject(rootId);
		paream.setObject(leftNum);
		this.jdbcHelper.update(sql, paream);
	}

	/**
	 * 减少节点
	 * @param rootId
	 * @param leftNum
	 * @param rightNum
	 * @param minusNum
	 * @throws SQLException
	 */
	public void minusLeftAndRight(Object rootId, Long leftNum, Long rightNum, Long minusNum) throws SQLException {
		String sql = "UPDATE " + tableName + " SET right_num = right_num - " + minusNum + " WHERE root_id = ? AND right_num > ?";

		Parameters paream = new Parameters();
		paream.setObject(rootId);
		paream.setObject(rightNum);
		this.jdbcHelper.update(sql, paream);

		sql = "UPDATE " + tableName + " SET left_num = left_num - " + minusNum + " WHERE root_id = ? AND left_num > ?";

		paream = new Parameters();
		paream.setObject(rootId);
		paream.setObject(leftNum);
		this.jdbcHelper.update(sql, paream);
	}

	public void updateRootId(Object id, Object rootId) throws SQLException {
		String sql = "UPDATE " + tableName + " SET root_id = ? WHERE id = ?";
		Parameters paream = new Parameters();
		paream.setObject(rootId);
		paream.setObject(id);
		this.jdbcHelper.update(sql, paream);
	}

	public void updateRootId(Object oldRootId, Long leftNum, Long rightNum, Object rootId, Long parentLeftNum) throws SQLException {
		Long num = parentLeftNum - leftNum + 1;
		String sql = "UPDATE " + tableName + " SET root_id = ?,left_num = left_num + " + num + ",right_num = right_num + " + num
				+ " WHERE root_id = ? AND left_num >= ? AND right_num <= ?";
		Parameters paream = new Parameters();
		paream.setObject(rootId);
		paream.setObject(oldRootId);
		paream.setObject(leftNum);
		paream.setObject(rightNum);
		this.jdbcHelper.update(sql, paream);
	}

}
